<?php

Page::addJS('jquery-1.2.2.pack');

    // элемент выводит список радио-батонов, основываясь на переданных параметрах

if (! isset($checksNumber)) {
    $checksNumber = 1;
}

$js_data = array(
    "env" => array(
        "po" => array(
            "selectAllAnsfers" => __d("validation", "answers_validation_error", true),
            "youNotFillNextAnsfers" => __d("validation", "You did not answer the following questions: ", true),
            "ContimueConfirm" => __d("tests", "confirm_test_blank_filling", true),
        ),
        "opposite_mode" => ($mode == "OPPOSITE"),
        "bin_combs_mode" => ($mode == "BIN_COMBS"),
        'binaryCombinationsQuestions' => $binaryCombinationsQuestions,
        'binaryCombinations' => $binaryCombinations,
        "questions_num" => $questionsNumber,
        "answersNumber" => $answersNumber,
        "checksNumber" => $checksNumber,
        "is_quest" => $type == "QUEST",
    ),
);

vendor('json');

?><?=$javascript->codeBlock("var __fillInGUI = ".json::drawToComment($js_data).";");?><?php
?><?=$javascript->link('fill_in.js');?><?php

/**
 * Converts number to corresponding uppercase list letter (1 => A, 2 => B, 3 => C, etc)
 *
 * @param integer $num
 * @return string
 */
function toL($num) {
    return chr(ord('A') + $num);
}

$url_path = '';

for ($i = 0; $i < $questionsNumber; $i++) {
    if ($i % $inGroupNumber == 0)  {
        ?><table class="table answer_table" cellpadding="0"><?php
        ?><tr><?php
            ?><th></th><?php
            if ($checksNumber > 1) {
                ?><th></th><?php
            }
            for ($j = 0; $j < $answersNumber; $j++) {
                ?><th><?=toL($j);?></th><?php
            }
        ?><tr><?php
    }
    if ($checksNumber > 1) {
        for ($k = 0 ; $k < $checksNumber; $k++) {
            $add_class = ! empty($ivalid_questions[$i+1][$k+1]) ? ' invalid' : '';
            ?><tr id="question_<?=($i+1);?>_<?=($k+1);?>" class="question<?=$add_class;?>"><?php
                if (! $k) {
                    ?><td class="number align-r" rowspan="<?=$checksNumber;?>"><?php
                        ?><?=($i+1);?><?php
                    ?></td><?php
                }
                ?><td><?php
                    ?><?=__d("tests", "blank_quest_".($k ? "least" : "most"), true);?><?php
                ?></td><?
                for ($j = 0 ; $j < $answersNumber; $j++) {
                    $td_class = "answer_cell";
                    if (isset($this->data['answers'][$i+1][$k+1]) && $this->data['answers'][$i+1][$k+1] == ($j+1)) {
                        $td_class .= " selected_answer";
                    }
                    ?><td class="<?=$td_class;?>" onmouseover="__fillInGUI.over(this, event);" onmouseout="__fillInGUI.out(this, event);" onclick="__fillInGUI.check(this, event)"><?php
                        ?><?=toL($j);?><?php
                        $checked = (isset($this->data['answers'][$i+1][$k+1]) && $this->data['answers'][$i+1][$k+1] == ($j+1)) ? ' checked="true"' : "";
                        ?><input type="radio" onmousedown="__fillInGUI.radioDown(this, event);" name="data[answers][<?=($i+1);?>][<?=($k+1);?>]" value="<?=($j+1);?>"<?=$checked;?>/><?php
                    ?></td><?php
                }
            ?></tr><?php
        }
    } else {
        $add_class = ! empty($ivalid_questions[$i+1]) ? ' invalid' : '';
        ?><tr id="question_<?=($i+1);?>" class="question<?=$add_class;?>"><?php
            ?><td class="number align-r"><?php
                ?><?=($i+1);?><?php
            ?></td><?php
            for ($j = 0 ; $j < $answersNumber; $j++) {
                $td_class = "answer_cell";
                if (isset($this->data['answers'][$i+1]) && $this->data['answers'][$i+1] == ($j+1)) {
                    $td_class .= " selected_answer";
                }
                ?><td class="<?=$td_class;?>" onmouseover="__fillInGUI.over(this, event);" onmouseout="__fillInGUI.out(this, event);" onclick="__fillInGUI.check(this, event)"><?php
                    ?><?=toL($j);?><?php
                    if (($mode == "BIN_COMBS") && in_array($i+1, $binaryCombinationsQuestions)) {
                        $checked = (isset($this->data['answers'][$i+1]) && is_array($this->data['answers'][$i+1]) && in_array($j+1, $this->data['answers'][$i+1])) ? ' checked="true"' : "";
                        ?><input type="radio" onmousedown="__fillInGUI.radioDown(this, event);" name="data[answers][<?=($i+1);?>][<?=($j+1);?>]" value="<?=($j+1);?>"<?=$checked;?>/><?php
                    } else {
                        $checked = (isset($this->data['answers'][$i+1]) && $this->data['answers'][$i+1] == ($j+1)) ? ' checked="true"' : "";
                        ?><input type="radio" onmousedown="__fillInGUI.radioDown(this, event);" name="data[answers][<?=($i+1);?>]" value="<?=($j+1);?>"<?=$checked;?>/><?php
                    }
                ?></td><?php
            }
        ?></tr><?php
    }

    if ($i % $inGroupNumber == $inGroupNumber - 1 || $i == $questionsNumber - 1 ) {
        ?></table><?php
    }
}

?>